package com.example.mercury.my.shop;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.mercury.R;
import com.example.mercury.entity.Shop;
import com.example.mercury.entity.User;
import com.example.mercury.main.CategoryActivity;
import com.example.mercury.my.adapter.ShopAdapter;
import com.example.mercury.utils.HttpUtils;
import com.example.mercury.utils.JsonUtils;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @author 盛守义
 * @createTime 2021/11/12 14:25
 * @projectName TurningCoffee
 * @className ShopActivity.java
 * @description TODO
 */
public class ShopActivity extends Activity {
    private TextView tv_score;
    private ListView lv_shop;
    private ShopAdapter adapter;
    private Shop shops;

    private int coffeeBeans;
    private User user;
    private SharedPreferences sharedPreferences;

    private Activity activity;

    private HttpUtils httpUtils;
    private JsonUtils jsonUtils;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity_shop);

        initViews();
        getData();


        lv_shop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });

    }


    void getData(){
        //咖啡豆
        sharedPreferences = getSharedPreferences("user", 0);
        coffeeBeans = sharedPreferences.getInt("coffeeBean", 0);
        Log.e("ShopActivity", "shareCoffeeBean: "+coffeeBeans );
        tv_score.setText(coffeeBeans+" 个");
        user = (User)getIntent().getSerializableExtra("userInfo");

        Call call = httpUtils.getCall("/user/center-mall/mallList");
        //Call call = httpUtils.getCall("/user/center-mall/mallList");
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                ShopActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ShopActivity.this, "网络连接崩溃了，绝对绝对不是我们的错", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String res = response.body().string();
                Log.e("ShopActivity", "onResponse: "+res );
                ShopActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        shops = jsonUtils.getShop(res);
                        adapter = new ShopAdapter(shops.getData().getList(), ShopActivity.this, R.layout.my_list_shop, mListenter);
                        lv_shop.setAdapter(adapter);
                    }
                });
            }
        });

    }

    void initViews(){
        shops = new Shop();
        httpUtils = new HttpUtils();
        zjsonUtils = new JsonUtils();
        lv_shop = findViewById(R.id.lv_detailedshop);
        activity = ShopActivity.this;
    }

    private ShopAdapter.ShopClickListener mListenter = new ShopAdapter.ShopClickListener() {
        @Override
        public void myOnClick(int position, View v) {
            Log.e("ShopId", shops.getData().getList().get(position).getId()+"");
            String url = "/user/center-mall/mallItem/" + user.getData().getUser().getUserId() + "/" + shops.getData().getList().get(position).getId();
            Log.e("ShopActivity", "myOnClickURL: "+ url);
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), "");
            Call call = httpUtils.getCall(requestBody, url);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(activity, "网络连接崩溃了，绝对绝对不是我们的错", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    String res = response.body().string();
                    Log.e("CenterActivity","ShopReturn" + res);
                    Shop shop = jsonUtils.getShop(res);
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(shop.getMessage() == "兑换成功" || shop.getMessage().equals("兑换成功")){
                                int changedCoffeeBeans = coffeeBeans - shops.getData().getList().get(position).getMallBeans();
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.putInt("coffeeBean", changedCoffeeBeans);
                                editor.commit();
                                tv_score.setText(changedCoffeeBeans+" 个");
                                Intent intent = new Intent();
                                intent.setAction("shopCoffeeBean");
                                sendBroadcast(intent);
                            }else{
                                Toast.makeText(activity, "兑换失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            });
        }
    };

}
